package edu.fiu.cis.selfhealingdisk;

import java.io.PrintWriter;
import java.util.Map;


/**
 * @author vanvorst
 * 
 */
public class Main {
	public static final boolean DEBUG=System.getProperties().containsKey("DEBUG");

	/**
	 * @param args
	 */
	public static void main(final String[] args) {
		if (args.length < 3) {
			final String name = Main.class.getPackage().getName()
					+ Main.class.getSimpleName().replace(".java", "");
			System.out
					.println("Usage: java "
							+ name
							+ " <sim configuration> <io trace> <output prefix>");
							//+ " <sim configuration> <io trace> <output trace> <fault stats> <disk model> [<disk model> ... <disk model>]");
			System.exit(1);
		}
		try {
			System.out.println("Starting simulation.");System.out.flush();
			
			final Configuration config = new Configuration(args[0]);
			final IOTrace originalIOTrace = new IOTrace(args[1]);
			final ParityModel parityModel = ParityModel.createParityModel(config, originalIOTrace);
			//final FaultModel faultModel = new FaultModel(config);
			final SimulationExecutive simEx = new SimulationExecutive(parityModel, config,config.getDiskSimParFile(),config.getDiskSimOutFile());
			
			System.out.println("   Saving original (converted) IO trace to '"+args[1]+".disksim'.");System.out.flush();
			originalIOTrace.saveInDiskSimFormat(args[2]+"_orig", false);
			
			System.out.println("   Running simulation.");System.out.flush();
			Map<String,String> stats = simEx.runSimulation();
			
			System.out.println("   Saving the transformed trace to '"+args[2]+".blkparse'.");System.out.flush();
			parityModel.getTransformedTrace().saveInBlkParseFormat(args[2]+".blkparse",true);
			
			
			
			System.out.println("   Saving the final stats in '"+args[2]+".stats'.");System.out.flush();
			PrintWriter pw = new PrintWriter(args[2] + ".stats");
			for (String v : stats.keySet()) {
				pw.println(v + ":" + stats.get(v));
			}
			pw.close();
			
			
			
			
			//System.out.println("   Preforming fault analysis.");System.out.flush();
			//faultModel.analyze(parityModel.getTransformedTrace(), parityModel
		//			.getTransformedLayout(), args[3]);
			
			System.out.println("Finished.");
		} catch (final Exception e) {
			e.printStackTrace();
			System.out.flush(); System.out.flush();
			System.out.println("\nUnexpected error. Bailing!\n\n");
			e.printStackTrace(System.out);
			System.out.println("\n\n");
			System.exit(1);
		}
	}
}
